import { http } from "@/utils/http";
import { baseUrlApi } from "./utils";
export type UserResult = {
  success: boolean;
  message: string;
  data: {
    /** 头像 */
    avatar: string;
    /** 用户名 */
    username: string;
    /** 昵称 */
    nickname: string;
    /** 当前登录用户的角色 */
    roles: Array<string>;
    /** 按钮级别权限 */
    permissions: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};
type Result = {
  success: boolean;
  data: any;
  msg: string;
};
export type UserInfo = {
  /** 主键 */
  id: string;
  /** 头像 */
  avatar: string;
  /** 用户名 */
  username: string;
  /** 昵称 */
  nickname: string;
  /** 邮箱 */
  email: string;
  /** 联系电话 */
  phone: string;
  /** 简介 */
  description: string;
};

export type UserInfoResult = {
  success: boolean;
  data: UserInfo;
  msg: string;
};
export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};
type ResultTable = {
  success: boolean;
  data?: {
    /** 列表数据 */
    list: Array<any>;
    /** 总条目数 */
    total?: number;
    /** 每页显示条目个数 */
    pageSize?: number;
    /** 当前页数 */
    currentPage?: number;
  };
};
/** 登录 */
export const getLogin = (data?: object) => {
  return http.request<UserResult>("post", baseUrlApi("public/user/login"), {
    data
  });
};

/** 刷新`token` */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>(
    "post",
    baseUrlApi("public/user/refresh-token"),
    {
      data
    }
  );
};
/** 账户设置-个人信息 */
export const getMine = (params?: object) => {
  return http.request<UserInfoResult>(
    "get",
    baseUrlApi("public/user/getUserInfo"),
    { params }
  );
};

/** 账户设置-个人安全日志 */
export const getMineLogs = (data?: object) => {
  return http.request<ResultTable>("get", "/mine-logs", { data });
};
/** 文件上传 */
export const formUpload = data => {
  return http.request<Result>("post", baseUrlApi("public/user/uploadAvatar"), {
    data,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
};

/** 获取头像 */
export const getAvatar = (filename: string) => {
  return http.request<any>(
    "get",
    baseUrlApi(`public/user/getUserAvatar/${filename}`),
    {
      responseType: "blob"
    }
  );
};

/** 更新用户信息 */
export const updateUserInfo = (data?: object) => {
  return http.request<Result>(
    "post",
    baseUrlApi("public/user/updateUserInfo"),
    {
      data
    }
  );
};

/** 修改密码 */
export const updateUserPassword = (data?: object) => {
  return http.request<Result>(
    "post",
    baseUrlApi("public/user/updateUserPassword"),
    {
      data
    }
  );
};
